<?php

class Api_CategoryController extends Zend_Rest_Controller
{
    private $dbTable="category";
    
    public function init()
    {
        require_once 'Zend/Db.php';
        $this->db = Zend_Db::factory('Mysqli',
        array(
        'host' => 'localhost',
        'dbname' => 'zooxanth_mystuff',
        'username'=> 'zooxanth_mystuff',
        'password'=> 'wecandoit25')
        );
        //$this->db = Zend_Db::factory($config->resources->db);
        $this->no_results = array('status' => 'NO_RESULTS');
    }
    
    /**
     * List
     *
     * The index action handles index/list requests; it responds with a
     * list of the requested resources.
     * 
     * @return json
     */
    public function indexAction()
    {   
        $data = $this->db->fetchAll('SELECT * FROM '.$this->dbTable);
        /*
        foreach ($data as $row) {
            echo $row['realname'];
        }
         *
         */
        // do some processing...
        // Send the JSON response:
        //$this->getResponse()->setHeader('Content-type', 'application/json');
        $this->_helper->json($data);
    }
    // 1.9.2 fix
    public function listAction() { return $this->_forward('index'); }

    /**
     * View
     *
     * The get action handles GET requests and receives an 'id' parameter; it 
     * responds with the server resource state of the resource identified
     * by the 'id' value.
     * 
     * @param integer $id
     * @return json
     */
    public function getAction()
    {   
        if($this->_hasParam('parent')){
            $parent = $this->_getParam('parent');
            if($parent==0){
                $data = $this->db->fetchAll('SELECT * FROM '.$this->dbTable.' WHERE parent_category IS NULL');
            }else{
                $data = $this->db->fetchAll('SELECT * FROM '.$this->dbTable.' WHERE parent_category='.$parent);
            }
            $this->_helper->json($data);
        }else if ($this->_hasParam('id')){
            $id = $this->_getParam('id');
            $data = $this->db->fetchRow('SELECT * FROM '.$this->dbTable.' WHERE id='.$id);
            $this->_helper->json($data);
        }else{
            //Return nothing
            $this->_helper->json($this->no_results);
        }

 
    }
    
    public function showAction(){
        $this->_helper->json($this->no_results);
    }

    /**
     * Create
     *
     * The post action handles POST requests; it accepts and digests a
     * POSTed resource representation and persists the resource state.
     * 
     * @param integer $id
     * @return json
     */
    public function postAction()
    {
        $this->_helper->viewRenderer->setNoRender(true);
        // do some processing...
        // Send the JSON response:
    }

    /**
     * Update
     *
     * The put action handles PUT requests and receives an 'id' parameter; it 
     * updates the server resource state of the resource identified by 
     * the 'id' value.
     * 
     * @param integer $id
     * @return json
     */
    public function putAction()
    {
        $this->_helper->json($this->no_results);
    }

    /**
     * Delete
     *
     * The delete action handles DELETE requests and receives an 'id' 
     * parameter; it updates the server resource state of the resource
     * identified by the 'id' value.
     * 
     * @param integer $id
     * @return json
     */
    public function deleteAction()
    {
        $id = $this->_getParam('id', 0);

        // do some processing...
        // Send the JSON response:
        $this->_helper->json($this->no_results);
    }
}

